package com.ficus.road.maintain.strategy.allfactory;

import cn.hutool.core.util.StrUtil;
import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.util.MyDateUtil;
import com.google.common.base.Objects;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.ficus.road.maintain.core.model.HitStatusEnum.NEW;

/**
 * @author xiawen yang
 * @date 2022/2/15 下午3:40
 */
public class IceAndBridgeStrategy implements AllFactoryStrategy<IceAndBridgeProcessZH> {
    private final String type;
    private final HitAllFactoryDao hitAllFactoryDao;

//    public IceAndBridgeStrategy(HitAllFactoryDao hitAllFactoryDao) {
//        this.hitAllFactoryDao = hitAllFactoryDao;
//    }

    public IceAndBridgeStrategy(HitAllFactoryDao hitAllFactoryDao, String type) {
        this.hitAllFactoryDao = hitAllFactoryDao;
        this.type = type;
    }

    @Override
    public Hit convert2AllFactoryHit(IceAndBridgeProcessZH process) {
        Hit fetchHit = hitAllFactoryDao.fetch(process.getEventsid());
        String reportTimeStr = process.getReportTime();
        Date reportTime = MyDateUtil.strParseDate(reportTimeStr, "yyyy-MM-dd HH:mm:ss");
        Date endTime = null;
        if (StringUtils.isNotEmpty(process.getEndTime())) {
            endTime = MyDateUtil.strParseDate(process.getEndTime(), "yyyy-MM-dd HH:mm:ss");
        }
        String[] coordinates = process.getXy().split(",");
        String address = null;
        try {
            if (StringUtils.isNotEmpty(process.getDescription())) {
                address = StrUtil.subBetween(process.getDescription(), "[", "]");
            }
        } catch (Exception e) {
            address = process.getDescription();
        }
        GpsLocation gpsLocation = GpsLocation.builder().latitude(31.235870d).longitude(121.621148d).build();
        if (ObjectUtils.isNotEmpty(coordinates) && coordinates.length == 2) {
            gpsLocation = GpsLocation.builder()
                    .longitude(Double.parseDouble(coordinates[0].trim()))
                    .latitude(Double.parseDouble(coordinates[1].trim())).build();
        }
        if (ObjectUtils.isEmpty(fetchHit)) {
            Hit hit = new Hit();
            HitAttrs hitAttrs = new HitAttrs();
            hitAttrs.setConfirmation(Confirmation.builder().build());
            hit.setScene("CITY");
            hit.setAttrs(hitAttrs);
            hit.setCreationTime(new Date().getTime() / 1000);
            hit.setFirstHitTime(reportTime.getTime() / 1000);
            hit.setLastModificationTime(new Date().getTime() / 1000);
            hitAttrs.setRoadName(address);
            hitAttrs.getConfirmation().setStatus(Confirmation.ConfirmationStatus.CORRECT);
            hitAttrs.setFrameImageUrl(process.getImgUrl());
            hitAttrs.setSceneImage(process.getImgUrl());
            hitAttrs.setLimitHour(24);
            hitAttrs.setGpsLocation(gpsLocation);
            hit.setId(process.getEventsid());
            hit.setType(type);
            hit.setNeedPushOut(false);
            generateHistory(process, hit, reportTime, endTime);
            return hit;
        } else {
            fetchHit.setLastModificationTime(new Date().getTime() / 1000);
//            fetchHit.setScene("CITY");
            fetchHit.getAttrs().setLimitHour(24);
//            fetchHit.getAttrs().setGpsLocation(GpsLocation.builder().latitude(31.235870d).longitude(121.621148d).build());
            List<HistoryHit> list = fetchHit.getList();
            List<HitStatusEnum> collect = list.stream().map(history -> history.getAttrs().getStatus()).collect(Collectors.toList());
            HistoryHit historyHit = new HistoryHit();
            historyHit.setHitTime(reportTime.getTime() / 1000);
            HistoryHitAttrs historyHitAttrs = new HistoryHitAttrs();
            historyHitAttrs.setSceneImage(fetchHit.getAttrs().getSceneImage());
            historyHit.setAttrs(historyHitAttrs);
            historyHitAttrs.setGpsLocation(gpsLocation);
            int status = process.getStatus();
            if (status >> 3 == 1) {
                if (collect.contains(HitStatusEnum.CONFIRMED)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.CONFIRMED);
                historyHitAttrs.setStatus(HitStatusEnum.CONFIRMED);
                historyHit.setHitTime(endTime.getTime() / 1000);
            } else if (status >> 2 == 1) {
                if (collect.contains(HitStatusEnum.COMPLETE)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.COMPLETE);
                historyHitAttrs.setStatus(HitStatusEnum.COMPLETE);
                historyHit.setHitTime(endTime.getTime() / 1000);
            } else if (status >> 1 == 1) {
                if (collect.contains(HitStatusEnum.DISPATCHED)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.DISPATCHED);
                historyHitAttrs.setStatus(HitStatusEnum.DISPATCHED);
            } else {
                if (collect.contains(HitStatusEnum.NEW)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.NEW);
                historyHitAttrs.setStatus(HitStatusEnum.NEW);
            }
            list.add(0, historyHit);
            return fetchHit;
        }
    }

    private void generateHistory(IceAndBridgeProcessZH zh, Hit hit, Date reportTime, Date endTime) {
        List<HistoryHit> historyHits;
        HistoryHit newHit = generateHistoryFromHit(zh, hit, NEW, reportTime);
        HistoryHit dispatchedHit = generateHistoryFromHit(zh, hit, HitStatusEnum.DISPATCHED, reportTime);
        HistoryHit completeHit = generateHistoryFromHit(zh, hit, HitStatusEnum.COMPLETE, endTime);
        HistoryHit confirmedHit = generateHistoryFromHit(zh, hit, HitStatusEnum.CONFIRMED, endTime);
        if (zh.getStatus() >> 3 == 1) {
            // 已复核
            hit.getAttrs().setStatus(HitStatusEnum.CONFIRMED);
            historyHits = Arrays.asList(confirmedHit, completeHit, dispatchedHit, newHit);
        } else if (zh.getStatus() >> 2 == 1) {
            // 已处置
            hit.getAttrs().setStatus(HitStatusEnum.COMPLETE);
            historyHits = Arrays.asList(completeHit, dispatchedHit, newHit);
        } else if (zh.getStatus() >> 1 == 1) {
            // 已派单
            hit.getAttrs().setStatus(HitStatusEnum.DISPATCHED);
            historyHits = Arrays.asList(dispatchedHit, newHit);
        } else {
            // 已发现
            hit.getAttrs().setStatus(NEW);
            historyHits = Arrays.asList(newHit);
        }
        hit.setList(historyHits);
    }

    private HistoryHit generateHistoryFromHit(IceAndBridgeProcessZH zh, Hit hit, HitStatusEnum status, Date time) {
        HistoryHit historyHit = new HistoryHit();
        HistoryHitAttrs historyHitAttrs = new HistoryHitAttrs();
        historyHit.setAttrs(historyHitAttrs);
        // 不准
        historyHit.setHitTime(time.getTime() / 1000);
        historyHitAttrs.setStatus(status);
        if (Objects.equal(status, NEW)) {
            historyHitAttrs.setSceneImage(hit.getAttrs().getSceneImage());
        } else {
            historyHitAttrs.setSceneImage(zh.getRectificationUrl());
        }
        // historyHitAttrs.setGpsLocation(GpsLocation.builder().latitude(Double.parseDouble()));
        return historyHit;
    }
}
